const app = getApp()
Page({
  data: {
    actionSheetHidden: true,
    actionSheetItems: ['不友善内容', '色情低俗', '涉嫌网络暴力', '违法违规'],
    fileList: [],
    imageList:[],
    item:"",
    value:"",
  },
  onLoad(){
    console.log( "====>")
  },
  //  用于显示和隐藏ActionSheet
  actionSheetTap: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  //  点击“取消”按钮或ActionSheet的外部区域，会调用该函数
  actionSheetChange: function (e) {
    console.log('actionSheetChange')
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindItem1: function (e) {
    this.setData({
      item:"不友善内容",
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindItem2: function (e) {
    this.setData({
      item:"色情低俗",
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindItem3: function (e) {
    this.setData({
      item:"涉嫌网络暴力",
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindItem4: function (e) {
    this.setData({
      item:"违法违规",
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'http://localhost:3000/user/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success:(res)=> {
        // 上传完成需要更新 fileList
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.data });
        this.setData({ fileList:fileList});
        console.log(this.data.fileList);
      },
    });
  },
  deleteImg(event){
    const fileList = this.data.fileList;
    const index = event.detail.index;
    fileList.splice(index,1)
    this.setData({ fileList:fileList});
  },
  getValue(e){
    this.setData({
      value: e.detail.value
    }) 
    console.log(this.data.value);
  },
  submit(){
    const fileList = this.data.fileList;
    const imageList=[] ;
    for(let i=0;i<fileList.length;i++){
      imageList.push('../../demo/upload/'+fileList[i].url);
      this.setData({imageList:imageList})
      console.log(fileList[i].url);
    }
    console.log(!this.data.item,this.data.value=='');
    if(!this.data.item || this.data.value==''){
      wx.showToast({
        title: '请写明投诉原因',
        mask:true, 
        icon:'none',
      })
    }else{
    wx.request({
      url: 'http://localhost:3000/report',
      data: {
        reason:this.data.item,
        describe:this.data.value,
        image:this.data.imageList,
        userId:app.globalData.user.id
      },
      success: (res) => {
        console.log(app.globalData.user.id);
        console.log(res);
        if(res.data=="成功"){
          wx.showToast({
            title: '提交成功',
            mask:true, 
            icon:'success',
          })
        }else{
          wx.showToast({
            title: '提交失败',
            mask:true, 
            icon:'none',
          })
        }
      },
      fail: (res) => {
        console.log(res);
      },
    })
    }
  },
  onClickHide() {
    this.setData({ state: false });
    this.setData({ flag: false });
  },
});
